-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcsv_reader.cpp
More file actions
293 lines (235 loc) · 9.71 KB
/
Copy pathcsv_reader.cpp
File metadata and controls
293 lines (235 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// csv_reader.cpp
// example for "C++17 In Detail"
// by Bartlomiej Filipek
// 2018/2019
#include <algorithm>
#include <charconv>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <optional>
#include <string_view>
#include <string>
#include <utility>
#include <vector>
#include "scope_timer.h"
static const char* const CSV_EXTENSION = ".csv";
static constexpr char DEFAULT_DATE_DELIM = '-';
static constexpr char CSV_DELIM = ';';
namespace fs = std::filesystem;
[[nodiscard]] std::string GetFileContents(const fs::path& filePath) {
std::ifstream inFile{ filePath, std::ios::in | std::ios::binary };
if (!inFile)
throw std::runtime_error("Cannot open " + filePath.filename().string());
std::string str(static_cast<size_t>(fs::file_size(filePath)), 0);
inFile.read(str.data(), str.size());
if (!inFile)
throw std::runtime_error("Could not read the full contents from " + filePath.filename().string());
return str;
}
[[nodiscard]] std::vector<std::string_view> SplitString(std::string_view str, char delim) {
std::vector<std::string_view> output;
const auto last = str.end();
for (auto first = str.begin(), second = str.begin(); second != last && first != last; first = std::next(second)) {
second = std::find(first, last, delim);
// we might get empty string views here, but that's ok in the case of CSV reader
//output.emplace_back(str.substr(std::distance(str.begin(), first), std::distance(first, second)));
output.emplace_back(&*first, std::distance(first, second));
if (second == last)
break;
}
return output;
}
[[nodiscard]] std::vector<std::string_view> SplitLines(std::string_view str) {
auto lines = SplitString(str, '\n');
if (!lines.empty() && lines[0].back() == '\r') { // Windows CR conversion
for (auto &&line : lines) {
if (line.back() == '\r')
line.remove_suffix(1);
}
}
return lines;
}
template<typename T>
[[nodiscard]] std::optional<T> TryConvert(std::string_view sv) noexcept {
T value{ };
const auto last = sv.data() + sv.size();
const auto res = std::from_chars(sv.data(), last, value);
if (res.ec == std::errc{} && res.ptr == last)
return value;
return std::nullopt;
}
// as of Dec 2018 only MSVC supports floating point conversion
// this code might be removed when GCC/Clang handles it as well
#ifndef _MSC_VER
template<>
[[nodiscard]] std::optional<double> TryConvert(std::string_view sv) noexcept {
std::string str{ sv };
return ::atof(str.c_str());
}
#endif
class Date {
public:
enum class Format { DayMonthYear, YearMonthDay };
public:
Date() = default;
Date(uint8_t day, uint8_t month, uint16_t year) noexcept : mDay(day), mMonth(month), mYear(year) { }
explicit Date(std::string_view sv, char delim = DEFAULT_DATE_DELIM, Format fmt = Format::DayMonthYear);
bool IsInvalid() const noexcept {
return mDay == 0 || mMonth == 0 || mYear == 0 || mDay > 31 || mMonth > 12;
}
friend bool operator < (const Date& lhs, const Date& rhs) noexcept {
return std::tie(lhs.mYear, lhs.mMonth, lhs.mDay) < std::tie(rhs.mYear, rhs.mMonth, rhs.mDay);
}
friend bool operator <= (const Date& lhs, const Date& rhs) noexcept {
return std::tie(lhs.mYear, lhs.mMonth, lhs.mDay) <= std::tie(rhs.mYear, rhs.mMonth, rhs.mDay);
}
friend std::ostream& operator<<(std::ostream &os, const Date &d) noexcept {
return os << d.mDay << DEFAULT_DATE_DELIM << d.mMonth << DEFAULT_DATE_DELIM << d.mYear;
}
private:
uint8_t mDay{ 0 }; // 1...31, 0 is invalid
uint8_t mMonth{ 0 }; // 1...12, 0 is invalid
uint16_t mYear{ 0 }; // 0 means invalid
};
Date::Date(std::string_view sv, char delim, Date::Format fmt) {
const auto columns = SplitString(sv, delim);
if (columns.size() == 3) {
mDay = TryConvert<uint8_t>(columns[fmt == Format::DayMonthYear ? 0 : 2]).value_or(0);
mMonth = TryConvert<uint8_t>(columns[1]).value_or(0);
mYear = TryConvert<uint16_t>(columns[fmt == Format::DayMonthYear ? 2 : 0]).value_or(0);
if (IsInvalid())
throw std::runtime_error("Cannot convert date from " + std::string(sv));
}
else
throw std::runtime_error("Cannot convert date, wrong element count/format, " + std::string(sv));
}
class OrderRecord {
public:
OrderRecord() = default;
OrderRecord(Date date, std::string coupon, double unitPrice, double discount, unsigned int quantity) noexcept
: mDate(date)
, mCouponCode(std::move(coupon))
, mUnitPrice(unitPrice)
, mDiscount(discount)
, mQuantity(quantity)
{ }
double CalcRecordPrice() const noexcept { return mQuantity * (mUnitPrice*(1.0 - mDiscount)); }
bool CheckDate(const Date& startDate, const Date& endDate) const noexcept {
return (startDate.IsInvalid() || startDate <= mDate) && (endDate.IsInvalid() || mDate <= endDate);
}
public:
// not enum class so that we can easily use it as array index
enum Indices { DATE, COUPON, UNIT_PRICE, DISCOUNT, QUANTITY, ENUM_LENGTH };
private:
Date mDate;
std::string mCouponCode;
double mUnitPrice{ 0.0 };
double mDiscount{ 0.0 }; // 0... 1.0
unsigned int mQuantity{ 0 };
};
[[nodiscard]] OrderRecord LineToRecord(std::string_view sv) {
const auto columns = SplitString(sv, CSV_DELIM);
if (columns.size() == static_cast<size_t>(OrderRecord::ENUM_LENGTH)) { // assuming we also might encounter empty "columns"
const auto unitPrice = TryConvert<double>(columns[OrderRecord::UNIT_PRICE]);
const auto discount = TryConvert<double>(columns[OrderRecord::DISCOUNT]);
const auto quantity = TryConvert<unsigned int>(columns[OrderRecord::QUANTITY]);
if (unitPrice && discount && quantity) {
return { Date(columns[OrderRecord::DATE]),
std::string(columns[OrderRecord::COUPON]),
*unitPrice,
*discount,
*quantity };
}
}
throw std::runtime_error("Cannot convert Record from " + std::string(sv));
}
[[nodiscard]] std::vector<OrderRecord> LinesToRecords(const std::vector<std::string_view>& lines) {
//ScopeTimer _t(__func__);
std::vector<OrderRecord> outRecords;
std::transform(lines.begin(), lines.end(), std::back_inserter(outRecords), LineToRecord);
return outRecords;
}
[[nodiscard]] std::vector<OrderRecord> LoadRecords(const fs::path& filename) {
//ScopeTimer _t(__func__);
const auto content = GetFileContents(filename);
ScopeTimer _t("Parsing Strings", /*store*/true);
const auto lines = SplitLines(content);
return LinesToRecords(lines);
}
[[nodiscard]] double CalcTotalOrder(const std::vector<OrderRecord>& records, const Date& startDate, const Date& endDate) {
ScopeTimer _t(__func__, /*store*/true);
return std::accumulate(std::begin(records), std::end(records), 0.0,
[&startDate, &endDate](double val, const OrderRecord& rec) {
if (rec.CheckDate(startDate, endDate))
return val + rec.CalcRecordPrice();
else
return val;
}
);
}
bool IsCSVFile(const fs::path &p) {
return fs::is_regular_file(p) && p.extension() == CSV_EXTENSION;
}
[[nodiscard]] std::vector<fs::path> CollectPaths(const fs::path& startPath) {
std::vector<fs::path> paths;
fs::directory_iterator dirpos{ startPath };
std::copy_if(fs::begin(dirpos), fs::end(dirpos), std::back_inserter(paths), IsCSVFile);
return paths;
}
struct Result {
std::string mFilename;
double mSum{ 0.0 };
};
[[nodiscard]] std::vector<Result>
CalcResults(const std::vector<fs::path>& paths, Date startDate, Date endDate) {
ScopeTimer _t(__func__, /*store*/true);
std::vector<Result> results;
for (const auto& p : paths) {
const auto records = LoadRecords(p);
const auto totalValue = CalcTotalOrder(records, startDate, endDate);
results.push_back({ p.string(), totalValue });
}
return results;
}
void ShowResults(const std::vector<Result>& results, Date startDate, Date endDate) {
const size_t maxStringLen = std::accumulate(std::cbegin(results), std::cend(results), 0,
[](size_t l, const auto &result) { return std::max(l, result.mFilename.length()); }
);
std::cout << std::setw(maxStringLen + 1) << std::left << "Name Of File";
if (!startDate.IsInvalid() && !endDate.IsInvalid())
std::cout << " | Total Orders Value between " << startDate << " and " << endDate << '\n';
else if (!startDate.IsInvalid())
std::cout << " | Total Orders Value since " << startDate << '\n';
else
std::cout << " | Total Orders Value\n";
for (const auto& [fileName, sum] : results)
std::cout << std::setw(maxStringLen + 1) << std::left << fileName << " | " << std::fixed << std::setprecision(2) << sum << '\n';
ScopeTimer::ShowStoredResults();
}
int main(int argc, const char** argv) {
if (argc <= 1) {
std::cerr << "path (startDate) (endDate)\n";
return 1;
}
try {
const auto paths = CollectPaths(argv[1]);
if (paths.empty()) {
std::cout << "No files to process...\n";
return 0;
}
const auto startDate = argc > 2 ? Date(argv[2]) : Date();
const auto endDate = argc > 3 ? Date(argv[3]) : Date();
const auto results = CalcResults(paths, startDate, endDate);
ShowResults(results, startDate, endDate);
}
catch (const fs::filesystem_error& err) {
std::cerr << "filesystem error! " << err.what() << '\n';
}
catch (const std::runtime_error& err) {
std::cerr << "runtime error! " << err.what() << '\n';
}
return 0;
}